package mysql

import (
	"taobao-merchandise/taobaorpc/taobaorpc"

	"gorm.io/gorm"
)

type Goods struct {
	gorm.Model
	Title  string  `gorm:"type:varchar(30);not null;comment:'商品标题'"`
	Price  float32 `gorm:"type:decimal(10,2);not null;comment:'商品价格'"`
	Image  string  `gorm:"type:varchar(300);not null;comment:'商品图片'"`
	UserId int64   `gorm:"type:int(11);not null;comment:'用户ID'"`
}

func (g *Goods) GoodsAdd(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}

func (g *Goods) FindGoodsById(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).First(g).Error
}

func (g *Goods) GoodsUpdate(db *gorm.DB, id uint) error {
	return db.Debug().Where("id=?", id).Updates(g).Error
}

func (g *Goods) GoodsList(db *gorm.DB, in *taobaorpc.GoodsListRequest, lists []*taobaorpc.GoodsList) ([]*taobaorpc.GoodsList, error) {
	err := db.Debug().Model(g).Select("goods.id as goods_id,title,price,image").Scopes(Paginate(int(in.Page), int(in.Size))).Find(&lists).Error
	return lists, err
}

func (g *Goods) GoodsShow(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Find(g).Error
}

func (g *Goods) GoodsDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Delete(g).Error
}
